// pages/member-userJoinO/member-userJoinO.js
import api from '../../utils/api.js'
let app = getApp();
import {
    HTTP
} from "../../utils/http.js";
import { config } from '../../utils/config'
let http = new HTTP();

Page({
    /**
     * 页面的初始数据
     */
    data: {
        // 此页面 页面内容距最顶部的距离
        height: app.globalData.height * 2 + 20,
        imgUrl: config.imgUrl,
    },

    onLoad: function (options) {
        let nickName = wx.getStorageSync('nickName');
        this.setData({
            nickName
        })
        this.getAllOrganizationList()
    },

    //拿到用户小区的其他团
    getAllOrganizationList() {
        let e = null
        let id = wx.getStorageSync('user_id');
        let city = wx.getStorageSync('city_id');
        let city_id = city;

        http.request({
            url: api.getAllOrganizationList,
            data: {
                id,
                city_id
            },
            success: (res) => {
                console.log('拿到用户小区的其他团', res);
                if (res.error_code == 40001) {
                    this.setData({
                        organizationList: false
                    })
                } else if (res.code == 200) {
                    res.data.map((ele, index, arr) => {
                        return this.getSendStatus(ele.id, ele.client_id, ele, arr)
                    })

                    this.setData({
                        organizationList: true,
                        allData: res.data, //社区内所有的团
                    })
                }

            }
        });

    },
    /**用户申请入团
     * id————用户ID
     * rid————社团拥有者ID
     * tname————社团名
     * uname————用户名
     * organization_id————团id
     * apply_or_invite: 1——申请  2——邀请
     */
    userJoinOrganization(e) {
        let self = this;
        let user_id = wx.getStorageSync('user_id');
        let rid = e.currentTarget.dataset.rid;
        let tname = e.currentTarget.dataset.tname;
        let uname = wx.getStorageSync('nickName');
        let organization_id = e.currentTarget.dataset.organization_id;

        http.request({
            url: api.userJoingototuan,
            data: {
                id: user_id,
                rid,
                tname,
                uname,
                organization_id,
                apply_or_invite: 1
            },
            success: (res) => {
                console.log('11111111111', res);
                if (res.code == 200) {
                    wx.showToast({
                        title: '申请成功',
                        icon: 'success',
                        duration: 1500,
                        mask: true,
                        success: () => {
                            setTimeout(() => {
                                self.onLoad()
                            }, 2000)
                        }
                    })
                } else if (res.error_code == 40008) { //我曾经加入过这个团，但退出了这个团，就不让加入当前这个团了
                    wx.showToast({
                        title: '您不能加入曾经加入过的团了！',
                        icon: 'none',
                        duration: 1500,
                        mask: true,
                        success: () => {

                        }
                    })
                }
            }
        });
    },

    //用户申请入团后，当前页面改变 申请的展示
    //拿到发送者的状态  huo 邀请者的 状态
    /**
     * send_id——发送者ID
     * organization_id——发送团ID
     * user_id——发送对象ID
     */
    //"type": 1   类型 0不需要处理 1申请方 2批准方 3申请成功 4申请失败
    getSendStatus(organization_id, client_id, ele, arr) {
        http.request({
            url: api.getSendStatus,
            data: {
                send_id: wx.getStorageSync('user_id'),
                organization_id: organization_id,
                user_id: client_id
            },
            success: (res) => {
                console.log('用户发送邀请或者申请的状态', res);
                if (res.code == 200) {
                    let type = res.data[0].type
                    if (type == 1) {
                        ele.stautsIng = true
                    }
                    this.setData({
                        allData: arr
                    })
                }
            }
        });
    },

    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },


})